﻿
Imports System.Net

'App to FD
Public Class AppRespnose
    Public Class headers
        Public Const enableGzip As String = "X-APP-ENABLEGZIP"
    End Class

    Private Class values
        Public Const enableGzip As String = "1"
    End Class

    Public Shared Sub parseheaders(ByVal response As WebResponse, _
                                   ByRef gzipEnabled As Boolean)
        If debugassert(Not response Is Nothing, "WebResponse is nothing.") Then
            gzipEnabled = strsame(response.Headers(headers.enableGzip), values.enableGzip)
        Else
            gzipEnabled = False
        End If
    End Sub

    Public Shared Function gzipEnabled(ByVal response As WebResponse) As Boolean
        Dim enable As Boolean
        parseheaders(response, enable)
        Return enable
    End Function

    Public Shared Sub removeheaders(ByVal response As HttpListenerResponse)
        If debugassert(Not response Is Nothing) Then
            removeheader(response.Headers(), headers.enableGzip)
        End If
    End Sub

    Public Shared Sub removeheaders(ByVal context As HttpListenerContext)
        If debugassert(Not context Is Nothing) Then
            removeheaders(context.Response())
        End If
    End Sub

    Public Shared Sub removeheaders(ByVal context As httpContext)
        If debugassert(Not context Is Nothing) Then
            removeheaders(context.response())
        End If
    End Sub

    Public Shared Sub disableGzip(ByVal response As HttpListenerResponse)
        If debugassert(Not response Is Nothing, "HttpListenerResponse is nothing.") Then
            removeheader(response.Headers(), headers.enableGzip)
        End If
    End Sub

    Public Shared Sub disableGzip(ByVal context As HttpListenerContext)
        If debugassert(Not context Is Nothing, "HttpListenerContext is nothing.") Then
            disableGzip(context.Response)
        End If
    End Sub

    Public Shared Sub disableGzip(ByVal context As httpContext)
        If debugassert(Not context Is Nothing, "httpContext is nothing.") Then
            disableGzip(context.response())
        End If
    End Sub

    Public Shared Sub enableGzip(ByVal response As HttpListenerResponse)
        If debugassert(Not response Is Nothing, "HttpListenerResponse is nothing.") Then
            modifyheader(response.Headers(), headers.enableGzip, values.enableGzip)
        End If
    End Sub

    Public Shared Sub enableGzip(ByVal context As HttpListenerContext)
        If debugassert(Not context Is Nothing, "HttpListenerContext is nothing.") Then
            enableGzip(context.Response)
        End If
    End Sub

    Public Shared Sub enableGzip(ByVal context As httpContext)
        If debugassert(Not context Is Nothing, "httpContext is nothing.") Then
            enableGzip(context.response())
        End If
    End Sub
End Class

'FD to App
Public Class FDRequest
    Public Class headers
        Public Const requestIp As String = "X-FD-REQUESTIP"
        Public Const url As String = "X-FD-URL"
        Public Const traceID As String = "X-FD-TRACEID"
    End Class

    Public Shared Sub insertheaders(ByVal inRequest As HttpListenerRequest, ByVal outRequest As WebRequest)
        If debugassert(Not outRequest Is Nothing, "WebRequest is nothing.") Then
            modifyheader(outRequest.Headers(), headers.requestIp, requestIp(inRequest))
            modifyheader(outRequest.Headers(), headers.url, requestFullUrl(inRequest))
            modifyheader(outRequest.Headers(), headers.traceID, traceID(inRequest))
        End If
    End Sub

    Public Shared Sub insertheaders(ByVal context As HttpListenerContext, ByVal request As WebRequest)
        If debugassert(Not context Is Nothing, "HttpListenerContext is nothing.") Then
            insertheaders(context.Request(), request)
        End If
    End Sub

    Public Shared Sub insertheaders(ByVal context As httpContext, ByVal request As WebRequest)
        If debugassert(Not context Is Nothing, "httpContext is nothing.") Then
            insertheaders(context.request(), request)
        End If
    End Sub

    Public Shared Sub removeheaders(ByVal request As HttpWebRequest)
        If debugassert(Not request Is Nothing) Then
            removeheader(request.Headers(), headers.requestIp)
            removeheader(request.Headers(), headers.url)
            removeheader(request.Headers(), headers.traceID)
        End If
    End Sub

    Public Shared Sub parseheaders(ByVal request As HttpListenerRequest, _
                                   ByRef requestIp As String, _
                                   ByRef url As String, _
                                   ByRef traceID As String)
        If debugassert(Not request Is Nothing, "request is nothing.") Then
            requestIp = request.Headers(headers.requestIp)
            url = request.Headers(headers.url)
            traceID = request.Headers(headers.traceID)
        Else
            requestIp = Nothing
            url = Nothing
            traceID = Nothing
        End If
    End Sub

    Public Shared Sub parseheaders(ByVal context As httpContext)
        If debugassert(Not context Is Nothing, "context is nothing.") Then
            Dim requestIp As String = Nothing
            Dim url As String = Nothing
            Dim traceID As String = Nothing
            parseheaders(context.request(), requestIp, url, traceID)
            If Not isemptystring(requestIp) Then
                context.setRequestIp(requestIp)
            End If
            If Not isemptystring(url) Then
                context.setUrl(url)
            End If
            If Not isemptystring(traceID) Then
                context.setTraceID(traceID)
            End If
        End If
    End Sub
End Class

'FD to user
Public Class FDResponse
    Public Class headers
        Public Const viafrontdoor As String = "X-VIA-FRONTDOOR"
    End Class

    Public Shared Sub insertheaders(ByVal response As HttpListenerResponse)
        If debugassert(Not response Is Nothing, "response is nothing.") Then
            addheader(response.Headers(), HttpResponseHeader.Via, applicationSign())
            modifyheader(response.Headers(), FDResponse.headers.viafrontdoor, applicationSign())
        End If
    End Sub

    Public Shared Sub insertheaders(ByVal context As HttpListenerContext)
        If debugassert(Not context Is Nothing, "context is nothing.") Then
            insertheaders(context.Response())
        End If
    End Sub

    Public Shared Sub insertheaders(ByVal context As httpContext)
        If debugassert(Not context Is Nothing, "context is nothing.") Then
            insertheaders(context.response())
        End If
    End Sub
End Class

Public Class FDInternalRequest
    Private Shared Function generateRequestPath(ByVal p As String) As String
        Return network.pathSeperator + p
    End Function

    Private Shared Function generateInternalRequestPath(ByVal name As String) As String
        Return generateRequestPath("__frontdoor_" + name + "__")
    End Function

    Public Shared ReadOnly ver As String = generateInternalRequestPath("ver")
    Public Shared ReadOnly sample As String = generateInternalRequestPath("sample_output")
    Public Shared ReadOnly buildInfo As String = generateInternalRequestPath("buildinfo")
    Public Shared ReadOnly diagnostic As String = generateInternalRequestPath("diagnostic")
    Public Shared ReadOnly status As String = generateInternalRequestPath("status")
    Private Const _ping As String = "ping"
    Public Shared ReadOnly ping As String = generateRequestPath(_ping)
    Public Shared ReadOnly ping2 As String = generateInternalRequestPath(_ping)
End Class